unit Secaop;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, Grids, DBGrids, StdCtrls, Buttons, ExtCtrls, DB, DBLocals,
  SqlExpr, Provider, DBClient, DBLocal, Mask;

type
  TfrmLocSecao = class(TForm)
    btLocaliza: TBitBtn;
    DBGrid1: TDBGrid;
    btOK: TBitBtn;
    btCancela: TBitBtn;
    StsBr: TStatusBar;
    DtSrc: TDataSource;
    Bevel1: TBevel;
    Label1: TLabel;
    cbLocaliza: TComboBox;
    edtLoc: TEdit;
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure btLocalizaClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure edtProcEnter(Sender: TObject);
    procedure edtProcExit(Sender: TObject);
    procedure CBPesquisaKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure CBPesquisaEnter(Sender: TObject);
    procedure CBPesquisaExit(Sender: TObject);
    procedure edtLocEnter(Sender: TObject);
    procedure edtLocExit(Sender: TObject);
    procedure DBGrid1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    Constructor Create (AOwner: TComponent; DataSet: TSQLClientDataSet); reintroduce;
  end;

var
  frmLocSecao: TfrmLocSecao;

implementation

uses Rotinas, CadModelo, uDMTabelas;

{$R *.dfm}

procedure TfrmLocSecao.DBGrid1TitleClick(Column: TColumn);
begin
  with DtSrc.DataSet as TSQLClientDataSet do
    begin
      IndexFieldNames := Column.FieldName;
      cbLocaliza.ItemIndex := Column.Index;
      edtLoc.SetFocus;
    end;
end;

procedure TfrmLocSecao.btLocalizaClick(Sender: TObject);
begin
  with DtSrc.DataSet as TSQLClientDataSet do
  begin
    Close;
    if cbLocaliza.ItemIndex = 1 then
      CommandText := 'select codsecao,nomsecao from secoes where nomsecao starting with '+ chr(39)+
                          edtLoc.Text + chr(39) + ' order by nomsecao' + ';'
    else
      CommandText := 'select codsecao,nomsecao from secoes where codsecao starting with '+ chr(39)+
                          edtLoc.Text + chr(39) + ' order by codsecao' + ';';
    CommandType := ctquery;
    Open;
    btOK.Enabled := not IsEmpty;
    if IsEmpty then
      begin
        DBGrid1.Enabled := False;
        StsBr.SimpleText := Format(' Nunhum registro foi encontrado ' +
               'com " %s "', [edtLoc.Text]);
      end
    else
      begin
        DBGrid1.Enabled := True;
        StsBr.SimpleText := Format(' %d registro(s) encontrado(s) com ' +
               '" %s "', [RecordCount, edtLoc.Text]);
      end;
  end;
end;

constructor TfrmLocSecao.Create(AOwner: TComponent;
  DataSet: TSQLClientDataSet);
begin
  inherited Create(AOwner);  // Chama o contrutor da classe base
  DtSrc.DataSet := DataSet;  // Atribui o DataSet de Procura
end;

procedure TfrmLocSecao.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
    begin
      key:=#0;
      perform(wm_nextdlgctl,0,0);
    end;
  if key=#27 then
    if Btcancela.enabled then
      Btcancela.Click;
end;

procedure TfrmLocSecao.edtProcEnter(Sender: TObject);
begin
  CorEntrada(Sender);
end;

procedure TfrmLocSecao.edtProcExit(Sender: TObject);
begin
  CorSaida(Sender);
end;

procedure TfrmLocSecao.CBPesquisaKeyPress(Sender: TObject; var Key: Char);
begin
  if key<>#13
     then key:=#0;
end;

procedure TfrmLocSecao.FormShow(Sender: TObject);
begin
  edtLoc.SetFocus;
  cbLocaliza.ItemIndex := 1;
end;

procedure TfrmLocSecao.CBPesquisaEnter(Sender: TObject);
begin
  CorEntrada(Sender);
end;

procedure TfrmLocSecao.CBPesquisaExit(Sender: TObject);
begin
  CorSaida(Sender);
end;

procedure TfrmLocSecao.edtLocEnter(Sender: TObject);
begin
  CorEntrada(Sender);
  edtLoc.Text := '';
  if cbLocaliza.ItemIndex = 0 then
    edtLoc.MaxLength := 3
  else
    edtLoc.MaxLength := 15;
end;

procedure TfrmLocSecao.edtLocExit(Sender: TObject);
begin
  CorSaida(Sender);
  btLocaliza.SetFocus;
end;

procedure TfrmLocSecao.DBGrid1DblClick(Sender: TObject);
begin
  btOK.Click;
end;

end.
